<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>属性监听</title>
        <script src="https://unpkg.com/vue@3.5.13"></script>
        <style>
            .container {
                margin: 70px auto 0;
                text-align: center;
                width: 300px;
            }

            .subTitle {
                color: gray;
                font-size: 14px;
            }

            .title {
                font-size: 45px;
            }

            .input {
                width: 90%;
            }

            .inputContainer {
                text-align: left;
                margin-bottom: 20px;
            }

            .subContainer {
                text-align: left;
            }

            .field {
                font-size: 14px;
            }

            .input {
                border-radius: 6px;
                height: 25px;
                margin-top: 10px;
                border-color: silver;
                border-style: solid;
                background-color: cornsilk;
            }

            .tip {
                margin-top: 5px;
                font-size: 12px;
                color: gray;
            }

            .setting {
                font-size: 9px;
                color: black;
            }

            .label {
                font-size: 12px;
                margin-left: 5px;
                height: 20px;
                vertical-align: middle;
            }

            .checkbox {
                height: 20px;
                vertical-align: middle;
            }

            .btn {
                width: 50%;
                height: 40px;
                margin-top: 20px;
                background-color: #0095ff;
                color: #fff;
                border: none;
                border-radius: 4px;
            }
        </style>
    </head>
    <body>
        <div class="container" id="Application">
            <div class="container">
                <div class="subTitle">加入我们，让我们一起努力！</div>
                <h1 class="title">创建你的账号</h1>
                <div v-for="(item,index) in fields" :key="index" class="inputContainer">
                    <div class="field">{{ item.title }} <span v-if="item.required" style="color: red">*</span></div>
                    <input v-model="item.model" class="input" :type="item.type">
                    <div class="tip" v-if="index === 2">请输入6-16位字母数字组合</div>
                </div>
                <div class="subContainer">
                    <div class="setting">偏好设置</div>
                    <input v-model="receiveMsg" class="checkbox" type="checkbox"/><label
                        class="label">接收更新文件</label>
                </div>
                <button class="btn" @click="submit">创建账号</button>
            </div>
        </div>
        <script>
            const App = {
                data() {
                    return {
                        fields: [
                            {
                                title: '用户名',
                                type: 'text',
                                required: true,
                                model: ''
                            },
                            {
                                title: '邮箱',
                                type: 'email',
                                required: false,
                                model: ''
                            },
                            {
                                title: '密码',
                                type: 'password',
                                required: true,
                                model: ''
                            }
                        ],
                        receiveMsg: false
                    }
                },
                computed: {
                    name: {
                        get() {
                            return this.$data.fields[0].model
                        },
                        set(newValue) {
                            this.$data.fields[0].model = newValue
                        }
                    },
                    email: {
                        get() {
                            return this.$data.fields[1].model
                        },
                        set(newValue) {
                            this.$data.fields[1].model = newValue
                        }
                    },
                    password: {
                        get() {
                            return this.$data.fields[2].model
                        },
                        set(newValue) {
                            this.$data.fields[2].model = newValue
                        }
                    }
                },
                methods: {
                    emailCheck() {
                        return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(this.email)
                    },
                    submit() {
                        if (this.name.length === 0){
                            alert('请输入用户名')
                            return
                        }
                        if (this.password.length <= 6) {
                            alert('密码长度不能低于6位')
                            return
                        }
                        if (this.email.length > 0 && !this.emailCheck(this.email)){
                            alert('邮箱格式不正确')
                            return
                        }
                        alert('提交成功')
                    }
                }
            }
            Vue.createApp(App).mount('#Application')
        </script>
    </body>
</html>